using System;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
using ICP_MS.iMass.Properties;
using LicenseManager;

namespace ICP_MS.iMass;

public class FormMain : Form
{
	private AppManager appManager;

	[Export("Shell", typeof(ContainerControl))]
	private static ContainerControl Shell;

	private CommonData data = new CommonData();

	public static int timeFlag = 1;

	private IContainer components = null;

	public FormMain()
	{
		appManager = new AppManager();
		InitializeComponent();
		if (!base.DesignMode)
		{
			Shell = this;
			appManager.Shell = Shell;
			appManager.LoadExtensions();
		}
	}

	private void FormMain_Load(object sender, EventArgs e)
	{
		Text = "iMass (" + $"V{Assembly.GetExecutingAssembly().GetName().Version.ToString()}" + ")";
		InstrumentType instrumentType = new InstrumentType();
		string path = "//iMassConfig//InstrumentType.txt";
		try
		{
			using StreamReader sr = File.OpenText(Application.StartupPath + path);
			instrumentType.InstruType = int.Parse(sr.ReadLine());
		}
		catch (Exception)
		{
			instrumentType.InstruType = 0;
		}
		if (instrumentType != null)
		{
			switch (instrumentType.InstruType)
			{
			case 0:
				BackgroundImage = Resources.InstrumentMS;
				break;
			case 1:
				BackgroundImage = Resources.ICPMS1000;
				break;
			case 2:
				BackgroundImage = Resources.InstrumentMACY;
				break;
			}
		}
		data.Init();
		if (!Program.CheckLicense())
		{
			FormRegister formRegister = new FormRegister();
			if (formRegister.ShowDialog() == DialogResult.Cancel)
			{
				Close();
			}
		}
	}

	private void Timer_Tick(object sender, EventArgs e)
	{
		int i = 0;
		int j = 0;
		while (PowerManager.powerStatus.ReadPowerInstrument == null)
		{
			PowerManager.Instance.SelectInstrumentCode();
			Thread.Sleep(1);
			if (PowerManager.powerStatus.ReadPowerInstrument != null || i == 3)
			{
				break;
			}
			i++;
		}
		while (RFManager.rfs.ReadDetectorInstrument == null)
		{
			Detector.Instance.msDetectorRF.SelectInstrumentCode();
			Thread.Sleep(1);
			if (RFManager.rfs.ReadDetectorInstrument != null || j == 3)
			{
				break;
			}
			j++;
		}
		string DetectorInstrument = RFManager.rfs.ReadDetectorInstrument;
		string PowerInstrument = PowerManager.powerStatus.ReadPowerInstrument;
		bool PowerInstrumentStatus = PowerManager.powerStatus.ReadEncrypted;
		bool DetectorInstrumentStatus = RFManager.rfs.ReadEncrypted;
		if (DetectorInstrument != null && PowerInstrument != null && !DetectorInstrument.Equals(PowerInstrument) && !EncryptManager.CheckLicense2(2, DetectorInstrument))
		{
			base.Enabled = false;
		}
		else if ((PowerInstrument != null && DetectorInstrument == null && CheckPowerConnection() && !PowerInstrumentStatus) || (DetectorInstrument != null && PowerInstrument == null && CheckPowerConnection() && DetectorInstrumentStatus))
		{
			Log.InsertLog("unknown error", "unknown error，（Unable to determine the source of the error！）", "", Severity.Debug);
			base.Enabled = false;
		}
		else if ((CheckPowerConnection() && !PowerInstrumentStatus) || (CheckPowerConnection() && DetectorInstrumentStatus))
		{
			Log.InsertLog("unknown error", "unknown error，（Unable to determine the source of the error！）", "", Severity.Debug);
			base.Enabled = false;
		}
		else
		{
			base.Enabled = true;
		}
	}

	private bool CheckPowerConnection()
	{
		if (PowerManager.powerStatus.StatusOnline)
		{
			PowerManager.powerStatus.StatusOnline = false;
			PowerManager.powerStatus.StatusConnected = true;
			return true;
		}
		PowerManager.powerStatus.StatusConnected = false;
		return false;
	}

	private void FormMain_SizeChanged(object sender, EventArgs e)
	{
		Refresh();
	}

	private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
	{
		data.Dispose();
		appManager.Dispose();
	}

	private void FormMain_FormClosed(object sender, FormClosedEventArgs e)
	{
	}

	private void FormMain_Resize(object sender, EventArgs e)
	{
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ICP_MS.iMass.FormMain));
		base.SuspendLayout();
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
		this.AutoSize = true;
		this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
		base.ClientSize = new System.Drawing.Size(1055, 701);
		this.DoubleBuffered = true;
		base.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
		base.Icon = (System.Drawing.Icon)resources.GetObject("$this.Icon");
		base.IsMdiContainer = true;
		base.Margin = new System.Windows.Forms.Padding(4);
		base.Name = "FormMain";
		this.Text = "iMass";
		base.WindowState = System.Windows.Forms.FormWindowState.Maximized;
		base.FormClosing += new System.Windows.Forms.FormClosingEventHandler(FormMain_FormClosing);
		base.FormClosed += new System.Windows.Forms.FormClosedEventHandler(FormMain_FormClosed);
		base.Load += new System.EventHandler(FormMain_Load);
		base.SizeChanged += new System.EventHandler(FormMain_SizeChanged);
		base.Resize += new System.EventHandler(FormMain_Resize);
		base.ResumeLayout(false);
	}
}
